<?php
  
// Last Modified: $Id: data_valid_fns.php 92 2005-08-06 01:01:47Z denk $

function filled_out($form_vars)
{
  // test that each variable has a value
  foreach ($form_vars as $key => $value)
  {
     if (!isset($key) || ($value == '')) 
        return false;
  } 
  return true;
}

function print_error_list($required, $error_msg)
{
	foreach ($required as $reqtype => $value)
	{
		if (!isset($reqtype) || ($value == ''))
		echo $error_msg["$reqtype"];
	}
}

function valid_email($address)
{
  // check an email address is possibly valid
  if (ereg('^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$', $address))
    return true;
  else 
    return false;
}

function print_array($array) {
while ($element = each($array)) {
	echo $element['key'];
	echo ' - ';
	echo $element['value'];
	echo '<br />';
	}
}

function oddeven($x){
	if($x & 1) return "odd";
	else return "even";
}

/** 
* Reduce a string by the middle, keeps whole words together 
* 
* @param string $string 
* @param int $max (default 50) 
* @param string $replacement (default [...]) 
* @return string 
* @author david at ethinkn dot com 
* @author loic at xhtml dot ne 
* @author arne dot hartherz at gmx dot net 
*/ 

function strMiddleReduceWordSensitive ($string, $max = 50, $rep = '[...]') { 
	$strlen = strlen($string); 

   if ($strlen <= $max) 
		   return $string; 

	$lengthtokeep = $max - strlen($rep); 
	$start = 0; 
	$end = 0; 

   if (($lengthtokeep % 2) == 0) { 
			$start = $lengthtokeep / 2; 
			$end = $start; 
   } else { 
			$start = intval($lengthtokeep / 2); 
			$end = $start + 1; 
   } 

	$i = $start; 
	$tmp_string = $string; 
   while ($i < $strlen) { 
		   if ($tmp_string[$i] == ' ') { 
					$tmp_string = substr($tmp_string, 0, $i) . $rep; 
					$return = $tmp_string; 
		   } 
			$i++; 
   } 

	$i = $end; 
	$tmp_string = strrev ($string); 
   while ($i < $strlen) { 
		   if ($tmp_string[$i] == ' ') { 
					$tmp_string = substr($tmp_string, 0, $i); 
					$return .= strrev ($tmp_string); 
		   } 
			$i++; 
   } 
   return $return; 
   return substr($string, 0, $start) . $rep . substr($string, - $end); 
}


/*
 Here's a little bit of code to chop strings (with html tags) around a specified length while making sure no html tags are chopped. It also prevents chopping while a tag is still open.

 Note: this will only work in xhtml strict/transitional due to the checking of "/>" tags and the requirement of quotations in every value of a tag. It's also only been tested with the presence of br, img, and a tags, but it should work with the presence of any tag.
*/
function html_substr($posttext, $minimum_length, $length_offset) {
	// The approximate length you want the concatenated text to be $minimum_length
	// The variation in how long the text can be
   // in this example text length will be between 200-10=190 characters
   // and the character where the last tag ends $length_offset
	// Reset tag counter & quote checker
	$tag_counter = 0;
	$quotes_on = FALSE;
	// Check if the text is too long
	if (strlen($posttext) > $minimum_length) {
		  // Reset the tag_counter and pass through (part of) the entire text
		  for ($i = 0; $i < strlen($posttext); $i++) {
			   // Load the current character and the next one
			  // if the string has not arrived at the last character
			   $current_char = substr($posttext,$i,1);
			  if ($i < strlen($posttext) - 1) {
					$next_char = substr($posttext,$i + 1,1);
			  }
			  else {
					$next_char = "";
			  }
			   // First check if quotes are on
			   if (!$quotes_on) {
					// Check if it's a tag
				   // On a "<" add 3 if it's an opening tag (like <a href...)
				   // or add only 1 if it's an ending tag (like </a>)
					if ($current_char == "<") {
						if ($next_char == "/") {
												   $tag_counter++;
						}
						else {
							   $tag_counter = $tag_counter + 3;
						}
				   }
					// Slash signifies an ending (like </a> or ... />)
				   // substract 2
					if ($current_char == "/") $tag_counter = $tag_counter - 2;
					// On a ">" substract 1
					if ($current_char == ">") $tag_counter--;
					// If quotes are encountered, start ignoring the tags
				   // (for directory slashes)
					if ($current_char == "\"") $quotes_on = TRUE;
			  }
			  else {
					// IF quotes are encountered again, turn it back off
					if ($current_char == "\"") $quotes_on = FALSE;
			  }
								   
			   // Check if the counter has reached the minimum length yet,
			  // then wait for the tag_counter to become 0, and chop the string there
			   if ($i > $minimum_length - $length_offset && $tag_counter == 0) {
					$posttext = substr($posttext,0,$i + 1) . "...";
				   return $posttext;
			  }
		}
   }
				return $posttext;

 }
?>
